package cn.beforedark.yygh.hosp.service.impl;

import cn.beforedark.yygh.hosp.repository.DepartmentRepository;
import cn.beforedark.yygh.hosp.service.DepartmentService;
import cn.beforedark.yygh.model.hosp.Department;
import cn.beforedark.yygh.vo.hosp.DepartmentVo;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;


    @Override
    public void save(Map<String, Object> map) {
        Department department = JSONObject.parseObject(JSONObject.toJSONString(map), Department.class);
        String hoscode = department.getHoscode();
        String depcode = department.getDepcode();
        // getDepartmentByHoscodeAndDepcode(hoscode,depcode) 也行
        Department platformDepartment = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);

        if (platformDepartment == null) {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        } else {
            department.setId(platformDepartment.getId());
            department.setCreateTime(platformDepartment.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(platformDepartment.getIsDeleted());
            departmentRepository.save(department);
        }

    }

    @Override
    public Page<Department> getList(Map<String, Object> map) {
        int page = Integer.parseInt((String) map.get("page"));
        int limit = Integer.parseInt((String) map.get("limit"));
        String hoscode = (String) map.get("hoscode");

        // 查询条件 : x
        Department department = new Department();
        department.setHoscode(hoscode);
        department.setIsDeleted(0); // 未逻辑删除的条目
        Example<Department> x = Example.of(department);


        Pageable pageable = PageRequest.of(page - 1, limit);
        return departmentRepository.findAll(x, pageable);
    }

    @Override
    public void remove(Map<String, Object> map) {
        String hoscode = (String) map.get("hoscode");
        String depcode = (String) map.get("depcode");

        // 逻辑删除
        Department d = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        if (d != null) {
            d.setIsDeleted(1);
            d.setUpdateTime(new Date());
            departmentRepository.save(d);
        }
    }


    @Override
    public List<DepartmentVo> getDepartmentList(String hoscode) {
        // 将 old 转为 result
        List<Department> old = departmentRepository.findByHoscode(hoscode);
        Map<String, List<Department>> collect = old.stream().collect(Collectors.groupingBy(Department::getBigcode));

        ArrayList<DepartmentVo> result = new ArrayList<>();
        for (Map.Entry<String, List<Department>> entry : collect.entrySet()) {
            String bigCode = entry.getKey();
            List<Department> oldChildrenList = entry.getValue();
            DepartmentVo bigDepartmentVo = new DepartmentVo();
            ArrayList<DepartmentVo> newChildrenList = new ArrayList<>();
            for (Department oldChild : oldChildrenList) {
                DepartmentVo newChild = new DepartmentVo();
                String depcode = oldChild.getDepcode();
                String depname = oldChild.getDepname();
                newChild.setDepcode(depcode);
                newChild.setDepname(depname);
                newChildrenList.add(newChild);
            }
            bigDepartmentVo.setDepcode(bigCode);
            bigDepartmentVo.setDepname(oldChildrenList.get(0).getBigname());
            bigDepartmentVo.setChildren(newChildrenList);
            result.add(bigDepartmentVo);
        }

        return result;
    }

    @Override
    public Department getDepByHoscodeAndDepcode(String hoscode, String depcode) {
        return departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
    }

}
